package sample.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import sample.model.Result;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class HttpClientUtils {

    public static Result sendGetHttp(String url) {
        Result res = null;
        CloseableHttpClient httpClient = null;
        httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        try {
            HttpEntity entity = null;
            entity = httpClient.execute(httpGet).getEntity();
            String s = EntityUtils.toString(entity);
            JSON json = JSON.parseObject(s);
            res = JSONObject.toJavaObject(json, Result.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return res;
    }

    public static Result sendPostHttp(String url,String jsonentity ) {
        Result result = null;
        CloseableHttpClient closeableHttpClient = null;
        closeableHttpClient = HttpClients.createDefault();
        StringEntity se = null;
        try {
            se = new StringEntity(jsonentity);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        se.setContentType("text/json");
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
        se.setContentEncoding("utf-8");
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(se);
        try {
            CloseableHttpResponse execute = closeableHttpClient.execute(httpPost);
            HttpEntity entity = execute.getEntity();
            String s = EntityUtils.toString(entity);
            JSON json = JSON.parseObject(s);
            result = JSONObject.toJavaObject(json, Result.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static Result sendDelete(String url) {
        Result res = null;
        CloseableHttpClient httpClient = null;
        httpClient = HttpClients.createDefault();
        HttpDelete httpDelete = new HttpDelete(url);
        try {
            HttpEntity entity = null;
            entity = httpClient.execute(httpDelete).getEntity();
            String s = EntityUtils.toString(entity);
            JSON json = JSON.parseObject(s);
            res = JSONObject.toJavaObject(json, Result.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return res;

    }

//    public static Result sendPostHttp(String url){
//        ExecutorService service = Executors.newFixedThreadPool(10);
//        CloseableHttpClient httpClient = null;
//        httpClient = HttpClients.createDefault();
//        CloseableHttpClient finalHttpClient = httpClient;
//        Future<Result> submit = service.submit(new Callable<Result>());
//        return submit.get();
//    }
}
